package com.gagakuai.datasource.pooled;

import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/*
 * @program: settement
 * @description: 池状态
 * @author: houhong
 * @create: 2022-11-08 22:40
 */
public class PoolState {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(PoolConnection.class);

    protected PoolDataSource poolDataSource;

    /*
     * 空闲连接
     */
    private List<PoolConnection> idleList = new ArrayList<>(16);
    /*
     * 活跃中的连接
     */
    private List<PoolConnection> activeList = new ArrayList<>(16);


    /*================统计信息===============*/
    //请求次数
    protected long requestCount = 0;
    //总请求时间
    protected long accumulatedRequestTime = 0;
    /*
    *  累计超时时间
    */
    protected long accumulatedCheckoutTime = 0;
    /*
    *  过期连接数
    */
    protected long claimedOverdueConnectionCount = 0;

    protected long accumulatedCheckoutTimeOfOverdueConnections = 0;
    //总等待时间
    protected long accumulatedWaitTime = 0;
    //要等待的次数
    protected long hadToWaitCount = 0;
    //坏的连接次数
    protected long badConnectionCount = 0;

    public PoolState(PoolDataSource poolDataSource) {
        this.poolDataSource = poolDataSource;
    }


    public List<PoolConnection> getIdleList() {
        return idleList;
    }

    public void setIdleList(List<PoolConnection> idleList) {
        this.idleList = idleList;
    }

    public List<PoolConnection> getActiveList() {
        return activeList;
    }

    public void setActiveList(List<PoolConnection> activeList) {
        this.activeList = activeList;
    }

    public synchronized long getAverageOverdueCheckoutTime() {
        return claimedOverdueConnectionCount == 0 ? 0 : accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount;
    }
}